/*
 * @Descripttion: 
 * @Author: junhuafly
 * @Date: 2020-03-17 14:06:40
 * @LastEditors: junhuafly
 * @Email: junhuafly@163.com
 * @LastEditTime: 2020-04-13 18:05:19
 */
// pages/mine/coupon/index.js
import {
  userAPI
} from './../../../api/user'
import {
  returnStatus,
} from './../../../utils/index'

var appInst = getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: "0",
    showContent: true,
    coupons: []
  },
  onChange({
    detail
  }) {
    const index = detail.name
    console.log(index)
    const resultCoupons = this.couponsClone.filter(item => {
      return index == 0 || index != 0 && item.status == index
    })
    this.setData({
      coupons: resultCoupons
    })
  },
  toDetail(customerCouponId){
    this.eventChannel.emit('getCoupon', {customerCouponId});
    wx.navigateBack();
  },
  onChoose({currentTarget:{dataset:{id}}}){
    console.log('--------------------id----------------');
    console.log(id);
    console.log('------------------------------------');
    this.toDetail(id)
  },
  disCoupon(){
    this.toDetail(0)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function ({
    title,
    customerCouponId
  }) {
    appInst.setTitle(title)
    this.eventChannel = this.getOpenerEventChannel()
    this.eventChannel.on('params', ({coupons}) => {
      this.setData({
        customerCouponId,
        coupons
      })
      return
    });
    if (customerCouponId) return
    // 获取优惠券信息
    userAPI.queryCustomerCoupons().then(async _ => {
      const res = await returnStatus(_);
      if (res) {
        this.couponsClone = JSON.parse(JSON.stringify(res));
        this.setData({
          coupons: res
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})